<template>
	<view>
		<view class="my-settle-container">
			<label class="radio" @click="changeAllGoodsState">
				<radio :checked="isFulCheck" color="#18BC37"></radio>
				<text>全选</text>
			</label>
			<view class="amout-box">
				合计：<text class="amout">￥{{checkedGoodsAmount}}</text>
			</view>
			<view class="btn-settle" @click="settlement">
				结算({{checkedCount}})
			</view>
		</view>
	</view>
</template>

<script>
	import {mapGetters,mapMutations,mapState} from 'vuex';
	import {cerate_order} from '@/api/order-request.js';
	export default {
		computed:{
			...mapGetters('cart_m',['checkedCount','total','checkedGoodsAmount']),
			...mapState('user_m',['address','token']),
			...mapGetters('user_m',['addstr']),
			...mapState('cart_m',['cart']),
			isFulCheck(){
				return this.total == this.checkedCount
			},
		},
		data() {
			
			return {
				second:3,
				timer:null
			};
		},
		onLoad() {
					console.log(this.$store);
				},
		methods:{
			...mapMutations('cart_m',['updateAllGoodsState']),
			...mapMutations('user_m',['updateNavigateInfo']),
			
			changeAllGoodsState(){
				this.updateAllGoodsState(!this.isFulCheck);
			},
			settlement(){
				if(!this.checkedGoodsAmount) return uni.$showMsg('请选择要结算的商品');
				if(JSON.stringify(this.address) == '{}'){
					return uni.$showMsg('请选择收货地址')
				};
				if(!this.token) return this.delayNavigate();
				
				this.payOrder();
			},
			async payOrder(){
				const orderObj = {
					order_price:this.checkedGoodsAmount,
					consignee_addr:this.addstr,
					goods:this.cart.filter(item=>item.goods_state).map(item=>({goods_id:item.goods_id,goods_number:item.goods_count,goods_price:item.goods_price}))
				};
				const result = await cerate_order(orderObj);
				console.log(result);
			},
			showTips(second){
				uni.showToast({
					icon:'none',
					title:'请登录后在进行结算！' + second + '秒后自动跳转到到登录页面',
					mask:true,
					duration:2000
				})
			},
			delayNavigate(){
				// this.second = 3;
				this.timer = setInterval(()=>{
					this.second--;
					
					if(this.second <= 0){
						clearInterval(this.timer);
						this.second = 3;
						
						uni.switchTab({
							url:'/pages/my/my',
							success:()=> {
								this.updateNavigateInfo({
									navigateType:'switchTab',
									from:'/pages/cart/cart'
								})
							}
						});
						return 
					};
					this.showTips(this.second);
				},1000)
			}
		}
	}
</script>

<style lang="scss">
.my-settle-container{
	position: fixed;
	left: 0;
	bottom: 0;
	width: 100%;
	height: 50px;
	background-color: #2979FF;
	display: flex;
	justify-content: space-between;
	align-items: center;
	z-index: 999;
}
</style>
